﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using HandyControl.Data;

namespace WPFDemoMVVM.ViewModel
{
    public partial class Paging2ViewModel : ObservableObject
    {
        private int _totalItems;
        private int _itemsPerPage = 10;
        private int _currentPage = 1;
        private int _maxVisiblePages = 7;

        [ObservableProperty]
        private ObservableCollection<int> pageSizeOptions;

        //[ObservableProperty]
        //private int pageSize;

        [ObservableProperty]
        private string language;

        public int TotalItems
        {
            get => _totalItems;
            set
            {
                SetProperty(ref _totalItems, value);
                UpdatePagination();
            }
        }

        public int ItemsPerPage
        {
            get => _itemsPerPage;
            set
            {
                SetProperty(ref _itemsPerPage, value);
                UpdatePagination();
            }
        }

        public int CurrentPage
        {
            get => _currentPage;
            set
            {
                SetProperty(ref _currentPage, value);
                UpdatePagination();
            }
        }

        public ObservableCollection<PaginationPageNumber> PageNumbers { get; set; } = new();

        public ICommand NextPageCommand => new RelayCommand(() =>
        {
            if (CurrentPage < TotalPages)
                CurrentPage++;
            PageChanged.Invoke(CurrentPage, ItemsPerPage);
        });

        public ICommand PreviousPageCommand => new RelayCommand(() =>
        {
            if (CurrentPage > 1)
                CurrentPage--;
            PageChanged.Invoke(CurrentPage, ItemsPerPage);
        });

        public ICommand GoToPageCommand => new RelayCommand<int>(page =>
        {
            if (page != CurrentPage)
                CurrentPage = page;
            PageChanged.Invoke(CurrentPage, ItemsPerPage);
        });

        public ICommand CurrentPageChangedCommand { get; set; }

        public int TotalPages => (int)Math.Ceiling((double)TotalItems / ItemsPerPage);

        public bool CanGoNext => CurrentPage < TotalPages;
        public bool CanGoPrevious => CurrentPage > 1;

        public string SummaryText => Language == "zh-CN" ? $"共 {TotalItems} 条 / {TotalPages} 页" : $"total {TotalItems} items / {TotalPages} pages";

        public Paging2ViewModel()
        {
            Language = "zh-CN";
            PageSizeOptions = new ObservableCollection<int> { 10, 20, 50 };
            ItemsPerPage = PageSizeOptions[0];
            CurrentPage = 1;
            UpdatePagination();
        }

        private void UpdatePagination()
        {
            PageNumbers.Clear();
            int totalPages = TotalPages;

            if (totalPages <= _maxVisiblePages)
            {
                for (int i = 1; i <= totalPages; i++)
                    AddPageButton(i);
            }
            else
            {
                AddPageButton(1);

                if (CurrentPage > 4)
                    AddEllipsis();

                int start = Math.Max(2, CurrentPage - 1);
                int end = Math.Min(totalPages - 1, CurrentPage + 1);

                for (int i = start; i <= end; i++)
                    AddPageButton(i);

                if (CurrentPage < totalPages - 3)
                    AddEllipsis();

                AddPageButton(totalPages);
            }

            CurrentPageChangedCommand?.Execute(CurrentPage);
            OnPropertyChanged(nameof(SummaryText));
            OnPropertyChanged(nameof(CanGoNext));
            OnPropertyChanged(nameof(CanGoPrevious));
        }

        private void AddPageButton(int number)
        {
            PageNumbers.Add(new PaginationPageNumber
            {
                Display = number.ToString(),
                PageNumber = number,
                IsEnabled = number != CurrentPage
            });
        }

        private void AddEllipsis()
        {
            PageNumbers.Add(new PaginationPageNumber
            {
                Display = "...",
                PageNumber = -1,
                IsEnabled = false
            });
        }

        //partial void OnPageSizeChanged(int value)
        //{
        //    CurrentPage = 1;
        //    OnPropertyChanged(nameof(TotalPages));
        //    OnPropertyChanged(nameof(SummaryText));
        //    //GoToPageCommand(CurrentPage);
        //    UpdatePagination();
        //}


        partial void OnLanguageChanged(string value)
        {
            OnPropertyChanged(nameof(SummaryText));
        }

        public event Action<int, int> PageChanged;
    }

    public class PaginationPageNumber
    {
        public string Display { get; set; } // "1", "...", "5" 等
        public int PageNumber { get; set; }
        public bool IsEnabled { get; set; } = true;
    }
}
